package com.nextStep.service;

import com.nextStep.entity.DbConfig;
import com.nextStep.jdbcOper.DbExecuteDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service("dbExecute")
public class DbExecuteServiceImpl implements DbExecuteService {

    @Autowired
    @Qualifier("dbExecuteDao")
    private DbExecuteDao dbExecuteDao;
    @Override
    public List showTables(DbConfig dbConfig) {
        List list = null;
        try {
            list = dbExecuteDao.showTables(dbConfig);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List selectTable(DbConfig dbConfig,String tableName,Integer pageNum,Integer pageSize ){
        List list = null;
        try {
            list = dbExecuteDao.selectTable(dbConfig,tableName,pageNum,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List selectColumns(DbConfig dbConfig, String tableName) {
        List list = null;
        try {
            list = dbExecuteDao.selectColumns(dbConfig,tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Integer selectLimitSumNum(DbConfig dbConfig, String tableName, Integer pageSize) {
        Integer pageLimit = null;
        try {
            pageLimit = dbExecuteDao.selectLimitSumNum(dbConfig,tableName,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pageLimit;
    }

    @Override
    public List condTable(DbConfig dbConfig, String tableName,  String selectColumns, String dataSelectInput) {
        List list = null;
        try {
            list = dbExecuteDao.condTable(dbConfig,tableName,selectColumns,dataSelectInput);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteData(DbConfig dbConfig, String tableName, String idValue)  {
        try {
            return dbExecuteDao.deleteData(dbConfig,tableName,idValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<List> updateRow(DbConfig dbConfig, String tbName, String rowIdValue) {
        List list = null;
        try {
            list = dbExecuteDao.updateRow(dbConfig, tbName, rowIdValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public void test() {
        dbExecuteDao.test();
        System.out.println("test-service   "+dbExecuteDao);
    }

    @Override
    public int updateRowData(DbConfig dbConfig, String tbName, String[] arrId, String[] arrValue) {
        int status = 0;
        try {
            status = dbExecuteDao.updateRowData(dbConfig, tbName, arrId,arrValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return status;
    }

    @Override
    public Integer addRow(DbConfig dbConfig, String tbName, String[] arrId, String[] arrValue) {
        Integer status = 0;
        try {
            status = dbExecuteDao.addRow(dbConfig, tbName, arrId,arrValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return status;
    }

    @Override
    public boolean dbconfigTest(DbConfig dbConfig) {
        return dbExecuteDao.dbconfigTest(dbConfig);
    }

    @Override
    public List<List> runSql(DbConfig dbConfig, String sql) {
        List<List> retuList = null;
        try {
            retuList = dbExecuteDao.runSql(dbConfig, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return retuList;
    }
}
